package com.kerbores.meeting.module.web;

import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.mvc.View;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import com.kerbores.meeting.biz.IDepartmentService;
import com.kerbores.meeting.biz.IRoleService;
import com.kerbores.meeting.domain.Department;
import com.kerbores.meeting.domain.Role;
import com.kerbores.meeting.module.base.BaseModule;
import com.kerbores.utils.entries.Result;
import com.kerbores.utils.web.pager.Pager;

/**
 * @author Kerbores
 *
 *         Create At 2015年1月16日 下午2:19:53
 */
@At("permission")
public class PermissionModule extends BaseModule {

	@Inject
	private IDepartmentService departmentService;

	@Inject
	private IRoleService roleService;

	@At
	@Ok("vel:permission/role_list.vm")
	public Result roleList(int page) {
		Pager<Role> pager = roleService.findBypage(_fixPage(page), pageSize);
		pager.setUrl(_base() + "/permission/roleList");
		return Result.success().addData("pager", pager).setTitle("角色列表");
	}

	@At
	public Result permissionList() {

		return Result.success();
	}

	@At
	public Result positionList() {

		return Result.success();
	}

	@At
	@Ok("vel:permission/department_list.vm")
	public Result departmentList(int page) {
		Pager<Department> pager = departmentService.findByPage(_fixPage(page), pageSize);
		pager.setUrl(_base() + "/permission/departmentList");
		return Result.success().addData("pager", pager).setTitle("部门列表");
	}

	@At("/department/add")
	@GET
	@Ok("vel:permission/department_add.vm")
	public Result addDepartment(String error) {
		return Result.success().setTitle("添加部门").addData("error", error);
	}

	@At("/department/add")
	@POST
	public View addDepartment(@Param("..") Department department) {
		boolean flag = departmentService.save(department);
		if (flag) {
			return _renderRedirct("/permission/departmentList");
		} else {
			return _renderRedirct("/permission/department/add?error=添加部门失败");
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.kerbores.nutz.module.base.AbstractBaseModule#_getNameSpace()
	 */
	@Override
	public String _getNameSpace() {
		return "permission";
	}

}
